package com.apobates.forum.toro.tuple.strategy;

import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 使用CompletableFuture实现ASF Lang3 Pair结构映射策略
 * @param <L> 左对象类型
 * @param <R> 右对象类型
 * @param <U> 右函数的参数类型
 *
 * @author xiaofanku@live.cn
 * @since 20220517
 */
public class PairSupplyFutureStrategy<L,R,U> extends AbstractPairFutureStrategy<L,R,U> implements PairSupplyStrategy<L,R,U> {
    private final static Logger logger = LoggerFactory.getLogger(PairSupplyFutureStrategy.class);

    public PairSupplyFutureStrategy(int timeout, TimeUnit unit) {
        super(timeout, unit);
    }

    public PairSupplyFutureStrategy() {
        super();
    }

    @Override
    public Pair<L, R> apply(Supplier<L> leftObjFun, Function<U, R> rightFun, Map<String, String> arguments) {
        CompletableFuture<L> lFuture = CompletableFuture.supplyAsync(()->leftObjFun.get()).completeOnTimeout(null, getTimeout(), getUnit());
        return super.get(lFuture, rightFun, arguments);
    }
}
